package com.ZeroCarbon.controller;

import com.ZeroCarbon.domain.dto.PostEditDTO;
import com.ZeroCarbon.domain.dto.PostReceiverDTO;
import com.ZeroCarbon.domain.dto.ReplyDTO;
import com.ZeroCarbon.domain.po.Comment;
import com.ZeroCarbon.domain.po.Reply;
import com.ZeroCarbon.entity.Result;
import com.ZeroCarbon.service.ICommentService;
import com.ZeroCarbon.service.IPostService;
import com.ZeroCarbon.service.IReplyService;
import com.ZeroCarbon.service.IUserPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 论坛信息控制器
 *
 * @since 2024-10-26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/forum")
public class ForumController {

    private final IPostService postService;

    private final ICommentService commentService;

    private final IUserPostService userPostService;

    private final IReplyService replyService;

    /**
     * 新增记录
     *
     * @param post 记录内容
     */
    @PostMapping("/savePost")
    public Result savePost(PostReceiverDTO post) {
        try {
            postService.savePost(post);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 随机获取记录
     */
    @GetMapping("/getPosts")
    public Result getPost(@RequestParam int type, @RequestParam boolean isFavorite) {
        try {
            return Result.success(postService.getRandomPost(type, isFavorite));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据记录id查找头像和用户名
     *
     * @param id 记录id
     */
    @GetMapping("/getAvatarAndUsernameById")
    public Result getAvatarAndUsernameById(@RequestParam String id) {
        try {
            return Result.success(userPostService.getAvatarAndUsername(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 给记录点赞
     *
     * @param id 记录id
     */
    @GetMapping("/thumbsUp")
    public Result thumbsUp(@RequestParam String id) {
        try {
            return Result.success(postService.thumbsUpPost(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 给记录收藏
     *
     * @param id 记录id
     */
    @GetMapping("/collection")
    public Result collection(@RequestParam String id) {
        try {
            //获取收藏数
            return Result.success(postService.collectPost(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }

    }

    /**
     * 判断是否收藏记录
     */
    @GetMapping("/isFavorite")
    public Result isFavorite(@RequestParam String id) {
        try {
            return Result.success(postService.isFavorite(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取记录详细信息
     *
     * @param id 记录id
     */
    @GetMapping("/getDetailPost")
    public Result getDetailPost(@RequestParam String id) {
        try {
            return Result.success(postService.getDetailPost(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 发表评论
     *
     * @param comment 评论
     */
    @PostMapping("/PostComment")
    public Result PostComment(@RequestBody Comment comment) {
        try {
            commentService.postComment(comment);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除评论
     *
     * @param id 评论id
     */
    @GetMapping("/deletePostComment")
    public Result deleteComment(@RequestParam String id) {
        try {
            commentService.deleteComment(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取到记录评论
     *
     * @param id 记录id
     */
    @GetMapping("/getComments")
    public Result getComment(@RequestParam String id) {
        try {
            return Result.success(commentService.getComments(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 给记录评论点赞
     *
     * @param id 评论id
     */
    @GetMapping("/thumbsUpComment")
    public Result thumbsUpComment(@RequestParam String id) {
        try {
            return Result.success(commentService.thumbsUpComment(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 判断当前用户是否对评论点赞
     *
     * @param id 评论id
     */
    @GetMapping("/isUserLikeComment")
    public Result isUserLikeComment(String id) {
        try {
            return Result.success(commentService.isUserLikeComment(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 回复评论
     */
    @PostMapping("/replyComment")
    public Result replyComment(@RequestBody Reply reply) {
        try {
            replyService.replyComment(reply);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取所有回复
     *
     * @param id 评论id
     */
    @GetMapping("/getSubComments")
    public Result getSubComments(@RequestParam String id) {
        try {
            List<ReplyDTO> subComments = replyService.getSubComments(id);
            //返回回复集合
            return Result.success(subComments);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 给回复点赞
     *
     * @param id 回复id
     */
    @GetMapping("/thumbsUpReply")
    public Result thumbsUpReply(@RequestParam String id) {
        try {
            int thumb = replyService.thumbsUpReply(id);
            //获取点赞数
            return Result.success(thumb);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 判断当前用户是否喜欢该回复
     */
    @GetMapping("/isUserLikeReply")
    public Result isUserLikeReply(@RequestParam String id) {
        try {
            return Result.success(replyService.isUserLikeReply(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除评论
     *
     * @param id 评论id
     */
    @GetMapping("/deleteReply")
    public Result deleteReply(@RequestParam String id) {
        try {
            replyService.deleteReply(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取当前用户所有记录
     */
    @GetMapping("/getCurrentPosts")
    public Result getCurrentPosts(@RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "6") int size) {
        try {
            return Result.success(postService.getCurrentPosts(page, size));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取修改记录信息
     *
     * @param id 获取编辑记录
     */
    @GetMapping("/getEditPost")
    public Result getEditPost(@RequestParam String id) {
        try {
            return Result.success(postService.getEditPost(id));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新编辑记录
     */
    @PostMapping("/updateEditPost")
    public Result updateEditPost(@RequestBody PostEditDTO postEditDTO) {
        try {
            postService.updateEditPost(postEditDTO);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除记录
     */
    @GetMapping("/deletePost")
    public Result deletePost(@RequestParam String id) {
        try {
            postService.deletePost(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取当前用户浏览记录
     */
    @GetMapping("/getHistory")
    public Result getHistory() {
        try {
            return Result.success(postService.getUserHistory());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 搜索帖子
     */
    @GetMapping("/searchPost")
    public Result searchGood(@RequestParam String search) {
        try {
            return Result.success(postService.searchPost(search));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
